package com.day16.ArrayList2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo2 {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();
        Student s1 = new Student("张三", 20);
        Student s2 = new Student("李四", 21);
        Student s3 = new Student("王五", 22);
        Student s4 = new Student("赵六", 23);
        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);
        Collections.sort(array, new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                return num;
            }
        });
        for (Student s : array) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}
